<?php if (!defined('THINK_PATH')) exit(); /*a:1:{s:50:"F:\chat/application/push\view\index\addfriend.html";i:1573109070;}*/ ?>
<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <META HTTP-EQUIV="pragma" content="no-cache">
    <META HTTP-EQUIV="Cache-Control" content="no-store, must-revalidate">
    <META HTTP-EQUIV="expires" content="0">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>添加好友</title>


    <link href="https://libs.cdnjs.net/twitter-bootstrap/4.3.1/css/bootstrap.css" rel="stylesheet">
    <link href="//at.alicdn.com/t/font_1494741_ocv8339blrb.css" rel="stylesheet">
    <link href="/public/chat/css/main.css" rel="stylesheet">
    <link href="/public/chat/css/friend.css" rel="stylesheet">

    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://libs.cdnjs.net/twitter-bootstrap/4.3.1/js/bootstrap.js"></script>
    <script src="https://libs.cdnjs.net/twitter-bootstrap/3.2.0/js/alert.js"></script>
    <script type="text/javascript" src="/public/vendor/layer/layer.js"></script>
    <script type="text/javascript" src="/public/chat/js/main.js"></script>



    <body onload="connect()">

        <div class="container">
            <div class="friend">
                <form action="" id="formdata" class="search">
                    <input type="text" name="" class="form-control" id="search_input" placeholder="请填写手机号码">
                    <input type="button" value="查找" class="btn btn-info" onclick="searchs()">
                </form>
                <div class="my">
                    我的手机号：<?php echo getSessInfo('account'); ?>
                </div>

                <ul class="list-group mt-3 hidden">
                    <li class="list-group-item" id="li_0">
                        <span class="head">A</span>
                        <span class="badge btn-info btn" onclick="adds(this)" data-id='0' data-step="0">添加</span>
                        <div>nickname</div>
                        <div>13000000000</div>
                        <textarea name="" id="" class="form-control hidden" placeholder="请输入添加理由"></textarea>
                    </li>
                </ul>
            </div>
        </div>
        <script>
            // 如果浏览器不支持websocket，会使用这个flash自动模拟websocket协议，此过程对开发者透明
            WEB_SOCKET_SWF_LOCATION = "/public/chat/swf/WebSocketMain.swf";
            // 开启flash的websocket debug
            WEB_SOCKET_DEBUG = true;

            var ws, name, client_list = {};

            // 连接服务端
            function connect() {
                console.log('1');
                // 创建websocket
                ws = new WebSocket("ws://" + document.domain + ":7272");
            }


            function searchs() {
                var keywords = $('#search_input').val();
                if (!keywords) {
                    layer.msg('请输入手机号码');
                    return false;
                }
                $.get("<?php echo url('getUserFriend'); ?>?key=" + keywords, function(res) {
                    $('.friend .list-group').empty();
                    layer.msg(res.msg)
                    if (res.code == 1) {
                        var html = '';
                        html += '<li class="list-group-item" id="li_' + res.data.id + '">';
                        html += '<span class="head">' + res.data.ide + '</span>';
                        html += ' <span class="badge btn-info btn" onclick="adds(this)" data-id="' + res.data.id + '" data-step="0">添加</span>';
                        html += ' <div class="nickname">' + res.data.nickname + '</div>';
                        html += ' <div class="account">' + res.data.account + '</div>';
                        html += ' <textarea name="" id="" class="form-control hidden" placeholder="请输入添加理由"></textarea>';
                        html += '</li>'
                        $('.friend .list-group').append(html).removeClass('hidden')
                    }
                })
            }

            function adds(e) {
                var id = $(e).data('id');
                var step = $(e).data('step');
                if (step == 0) {
                    $(e).text('确认添加').removeClass('btn-info').addClass('btn-warning');
                    $('#li_' + id).find('textarea').removeClass('hidden');
                    $(e).data('step', 1);
                }
                if (step == 1) {
                    var remark = $('#li_' + id).find('textarea').val();
                    if (!remark) {
                        layer.msg('请输入添加理由');
                        return false;
                    }
                    $.post("<?php echo url('addfriend'); ?>", {
                        id,
                        remark
                    }, function(res) {
                        layer.msg(res.msg);
                        if (res.code == 1) {
                            $('#li_' + id).find('.badge').removeClass('btn-warning').addClass('btn-default');
                        }

                        var user_id = "<?php echo getSessInfo('id'); ?>";
                        var ide = "<?php echo getSessInfo('ide'); ?>";
                        var nickname = "<?php echo getSessInfo('nickname'); ?>";
                        var data = {
                            type: "add_friend",
                            user_id,
                            other_id: id,
                            ide,
                            remark,
                            nickname
                        };
                        console.log(JSON.stringify(data));
                        ws.send(JSON.stringify(data));
                    })
                }

            }
        </script>
    </body>

</html>